package com.zhuoyue.web.controller.system.util;

import com.zhuoyue.web.controller.common.ControllerApiResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

//import io.jsonwebtoken.security.Keys;


/**
 * 设备Controller
 *
 * @author hewenze
 * @date 2020-08-07
 */
@Service
@Api(value = "设备接口", tags = "设备接口")

public class ApiTokenUtil {


    @Value("${spring.profiles.active}")
    private String env;

    @Value("${rest-template.ip}")
    private String ip;
    @Value("${rest-template.port}")
    private String port;


    public String getUrl() {
        //if ("prod".equals(env)) {
        //return "https://" + ip + ":";
        //} else {
        return "http://" + ip + ":" + port;
        // }
    }


    public String getApiToken(String account, String passWord, String orgCode, String type) {


        String user_url = getUrl() + "/smart/home/user/get/token?";
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders requestHeaders = new HttpHeaders();
        user_url = user_url + "account=" + account + "&password=" + passWord + "&type=" + "manage";
        requestHeaders.add("organizationCode", orgCode);
        org.springframework.http.HttpEntity<String> requestEntity = new org.springframework.http.HttpEntity<String>(null, requestHeaders);
        ResponseEntity<ControllerApiResult> response = restTemplate.exchange(user_url, HttpMethod.POST, requestEntity, ControllerApiResult.class);
        String tokenStr = response.getBody().getDataObject().toString();


        return "Bearer " + tokenStr;
    }


}
